package com.hp.ssmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.ssmp.dao.OrderMapper;
import com.hp.ssmp.entity.TOrder;
import com.hp.ssmp.entity.dto.DaysOrderNumDto;
import com.hp.ssmp.entity.dto.DaysOrderPriceDto;
import com.hp.ssmp.entity.dto.TOrderDto;
import com.hp.ssmp.service.OrderService;
import com.hp.ssmp.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service("orderService")
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, TOrder> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public boolean addOrder(TOrder order) {
        order.setOrdertime(new Date());
        return orderMapper.insert(order) > 0;
    }

    @Override
    public Page<TOrderDto> getOrderPage(Integer currentPage, Integer pageSize, Long orderid, String username, Long userid,Long orderstatus) {
        Page<TOrderDto> page = new Page<TOrderDto>(currentPage,pageSize);
        QueryWrapper<TOrderDto> wrapper = new QueryWrapper<TOrderDto>();
        if(null != orderid && !"".equals(orderid)){
            wrapper.eq("o.orderid",orderid);
        }
        if(null != username && !"".equals(username)){
            wrapper.like("u.username",username);
        }
        if(null != userid && !"".equals(userid)){
            wrapper.eq("o.userid",userid);
        }
        if(null != orderstatus && !"".equals(orderstatus)){
            wrapper.eq("o.orderstatus",orderstatus);
        }

        return orderMapper.getOrderDtoByPage(page,wrapper);
    }

    @Override
    public List<Integer> countOrderNum() {
        List<Integer> orderNum = orderMapper.getOrderNum();
        return orderNum;
    }

    @Override
    public HashMap<String, List<Object>> countOrderPrice(Integer day) {
        HashMap<String, List<Object>> stringListHashMap = new HashMap<String, List<Object>>();
        List<DaysOrderPriceDto> daysOrderPrice = orderMapper.getDaysOrderPrice(day);
        List<Object> dates=new ArrayList<Object>();
        List<Object> totalPrices=new ArrayList<Object>();
        for (DaysOrderPriceDto daysOrderPriceDto : daysOrderPrice) {
            dates.add(DateUtil.dateToString(daysOrderPriceDto.getDate(),"MM-dd"));
            totalPrices.add(daysOrderPriceDto.getTotalprice());
        }
        stringListHashMap.put("dates",dates);
        stringListHashMap.put("totalPrices",totalPrices);
        return stringListHashMap;
    }

    @Override
    public HashMap<String, List<Object>> countOrderNum(Integer day) {
        HashMap<String, List<Object>> stringListHashMap = new HashMap<String, List<Object>>();
        List<DaysOrderNumDto> daysOrderNum = orderMapper.getDaysOrderNum(day);
        List<Object> dates=new ArrayList<Object>();
        List<Object> nums=new ArrayList<Object>();
        for (DaysOrderNumDto daysOrderNumDto : daysOrderNum) {
//            dates.add(daysOrderNumDto.getDate());
            dates.add(DateUtil.dateToString(daysOrderNumDto.getDate(),"MM-dd"));
            nums.add(daysOrderNumDto.getNum());
        }
        stringListHashMap.put("dates",dates);
        stringListHashMap.put("nums",nums);
        return stringListHashMap;
    }

}
